package com.adtec.base.cloud.baseservice.rbac.system.controller;


import com.adtec.base.cloud.baseservice.rbac.system.service.SysDictService;
import com.adtec.base.cloud.commons.exception.AjaxResponse;
import com.adtec.base.cloud.persistence.system.model.SysDict;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 系统数据字典配置控制层代码
 *
 * @author 字母哥
 */
@RestController
@RequestMapping("/sysdict")
@Tag(name = "sysdict", description = "数据字典")
public class SysDictController {

    @Resource
    private SysDictService sysDictService;

    /**
     * 查询所有
     *
     * @return List<SysDict> 所有数据字典项
     */
    @PostMapping(value = "/all")
    public List<SysDict> all() {
        return sysDictService.all();
    }

    /**
     * 根据查询参数参训数据字典
     *
     * @param groupName 分组名称
     * @param groupCode 分组编码
     * @return 数据字典项列表
     */
    @PostMapping(value = "/query")
    @Operation(description = "数据字典：查询")
    @Parameters({
            @Parameter(name = "groupName", description = "分组名称(模糊查询)"),
            @Parameter(name = "groupCode", description = "分组编码(模糊查询)"),
    })
    public List<SysDict> query(
            @RequestParam(name = "groupName", required = false) String groupName,
            @RequestParam(name = "groupCode", required = false) String groupCode) {
        return sysDictService.query(
                groupName,
                groupCode);
    }

    /**
     * 根据id更新数据数据字典项目
     *
     * @param sysDict 更新实体（必须包含id）
     * @return 更新成功结果
     */
    @PostMapping(value = "/update")
    @Operation(description = "数据字典：更新")
    public AjaxResponse update(@RequestBody SysDict sysDict) {
        sysDictService.update(sysDict);
        return AjaxResponse.success("更新数据字典项成功！");
    }

    /**
     * 新增数据字典项
     *
     * @param sysDict 新增实体
     * @return 更新成功结果
     */
    @PostMapping(value = "/add")
    @Operation(description = "数据字典：新增")
    public AjaxResponse add(@RequestBody SysDict sysDict) {
        sysDictService.add(sysDict);
        return AjaxResponse.success("新增数据字典项成功！");
    }

    /**
     * 根据id删除数据字典项
     *
     * @param id 删除项id
     * @return 删除成功结果
     */
    @PostMapping(value = "/delete")
    @Operation(description = "数据字典：删除")
    @Parameters({
            @Parameter(name = "id", description = "字典ID"),
    })
    public AjaxResponse delete(@RequestParam Long id) {
        sysDictService.delete(id);
        return AjaxResponse.success("删除数据字典项成功!");
    }

}
